package cn.pugle.json.jackson;


import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.IOException;

/**
 * @author tzp
 * @since 2018/9/12
 */
@SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
@Data
public class Dog {
    @JsonProperty("the_name")//有这个可以不用getter/setter/@JsonSetter,可以重命名.注在field,method,constructor_param上
    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public Dog setAge(int age) {
        this.age = age;
        return this;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) throws IOException {
        Dog dog = new Dog();
        dog.name = "doggie";
        dog.age = 1;
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(dog));


        String json = "{\"age\":1,\"the_name\":\"doggie\"}";
        Dog dog1 = objectMapper.readValue(json, Dog.class);
        System.out.println(dog1);
    }
}
